package com.example.demo.router;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.config.CustomeParam;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

@Data
public class BaseRouter {
    protected String pagePath;
    protected String pageName;
    protected String pluginPath;
    protected String pluginName;
    protected String template;
    protected String title;
    protected String[] flagList;
    protected boolean isPage;

    @Autowired
    protected CustomeParam param;

    public BaseRouter() {
        this.flagList = new String[] { "head", "list", "edit", "info", "vue", "lazyload", "upload", "swiper", "qqmap", "encrypt" };
    }

    public String page() {
        return this.pagePath + this.pageName;
    }

    public String plugin() {
        return this.pluginPath + this.pluginName;
    }

    public String template() {
        return this.template;
    }

    public String fullPage() {
        return this.isPage ? page() : plugin();
    }

    public void initPage(String pageName, String metaFlag, String fileVersion, String title, Model model) {
        this.pageName = pageName;
        init(metaFlag, fileVersion, title, true, model);
    }

    public void initPlugin(String pluginName, String metaFlag, String fileVersion, String title, Model model) {
        this.pluginName = pluginName;
        init(metaFlag, fileVersion, title, false, model);
    }

    public void init(String metaFlag, String fileVersion, String title, boolean isPage, Model model) {
        this.isPage = isPage;

        String cssPath, jsPath;
        if (isPage) {
            cssPath = "/" + this.pagePath + "css/" + this.pageName + ".css";
            jsPath = "/" + this.pagePath + "js/" + this.pageName + ".js";
        }
        else {
            cssPath = "/" + this.pluginPath + "css/" + this.pluginName + ".css";
            jsPath = "/" + this.pluginPath + "js/" + this.pluginName + ".js";
        }

        URL cssUrl = this.getClass().getClassLoader().getResource("static" + cssPath);
        URL jsUrl = this.getClass().getClassLoader().getResource("static" + jsPath);

        JSONObject path = new JSONObject();
        path.put("cssFile", cssPath + "?v=" + fileVersion);
        path.put("jsFile", jsPath + "?v=" + fileVersion);

        JSONObject exist = new JSONObject();
        exist.put("cssFile", cssUrl != null);
        exist.put("jsFile", jsUrl != null);

        JSONObject flag = new JSONObject();
        List<String> list = Arrays.asList(metaFlag.split(","));
        for (int i = 0; i < this.flagList.length; i++) {
            flag.put(this.flagList[i], list.contains(this.flagList[i]));
        }

        JSONObject info = new JSONObject();
        info.put("title", title != null ? title : this.title);
        info.put("path", path);
        info.put("exist", exist);
        info.put("flag", flag);
        info.put("pageUrl", isPage ? page() : plugin());
        info.put("apiUrl", this.param.getVisitDomain());
        info.put("meta", this.pluginPath + "meta");

        model.addAttribute("info", info);
    }
}
